/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.unicode.unihan.parser.field;

import org.omg.CORBA.portable.ValueOutputStream;
import org.geergenbein.parser.ParserException;

/**
 *
 * @author Andrew
 */
public class IntegerParser implements FieldParser<Integer> {

    private int minValue;
    private int maxValue;

    public IntegerParser() {
        this(Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    public IntegerParser(int minValue, int maxValue) {
        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    public boolean isValid(String s) {
        boolean valid = false;
        try {
            parse(s);

        } catch (NumberFormatException e) {
            valid = false;
        } catch (ParserException e) {
            valid = false;
        }
        return valid;
    }

    public Integer parse(String s) {
        int value = Integer.parseInt(s);
        if (value < minValue || value > maxValue) {
            throw new ParserException();
        }
        return value;
    }

}
